package cn.t.web.android;

import cn.t.annotation.Runtime;
import cn.t.converter.user.UserQueryDTOConverter;
import cn.t.core.exception.BizException;
import cn.t.core.mode.Result;
import cn.t.dto.android.CallbackDTO;
import cn.t.dto.user.UserQueryDTO;
import cn.t.core.enums.base.CommonError;
import cn.t.enums.user.AndriodLoginTypeEnum;
import cn.t.facade.device.DeviceFacade;
import cn.t.facade.user.UserLoginFacade;
import cn.t.facade.user.UserQueryFacade;
import cn.t.runtime.dto.AndroidUserListRequest;
import cn.t.runtime.dto.AndroidUserListResponse;
import cn.t.runtime.dto.DeviceLoginRequest;
import cn.t.runtime.dto.DeviceLoginResponse;
import cn.t.vo.andriod.AndroidDeviceVO;
import cn.t.vo.user.UserSessionVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author l
 */
@Slf4j
@RestController
@RequestMapping("runtime/user")
@AllArgsConstructor
@Runtime
public class AndroidUserController {


    @PostMapping("/login")
    public DeviceLoginResponse login(@RequestBody DeviceLoginRequest loginRequest) {

        UserSessionVO userSession = null;

        switch (AndriodLoginTypeEnum.getByCode(loginRequest.getLoginType())) {
            case CARD:
                userSession = userLoginFacade.loginByCard(loginRequest.getDeviceId(), loginRequest.getCardCode(),AndriodLoginTypeEnum.getByCode(loginRequest.getLoginType()));
                break;
            case CARD_EMPLOYEE:
                userSession = userLoginFacade.loginByCardEmployee(loginRequest.getDeviceId(), loginRequest.getCardCode(), loginRequest.getPassword(),AndriodLoginTypeEnum.getByCode(loginRequest.getLoginType()));
                break;
            case USER_PASSWORD:
                userSession = userLoginFacade.loginByUserNamePassword(loginRequest.getDeviceId(), loginRequest.getUserName(), loginRequest.getPassword(),AndriodLoginTypeEnum.getByCode(loginRequest.getLoginType()));
                break;
            case EMPLOYEE_PASSWORD:
                userSession = userLoginFacade.loginByCardEmployeePassword(loginRequest.getDeviceId(), loginRequest.getEmployeeCode(), loginRequest.getPassword(),AndriodLoginTypeEnum.getByCode(loginRequest.getLoginType()));
                break;
            case FACE:
                userSession = userLoginFacade.loginByFace(loginRequest.getDeviceId(), loginRequest.getUserId(),AndriodLoginTypeEnum.getByCode(loginRequest.getLoginType()));
                break;
            case PHONE_PASSWORD:
            case ANDROID_ADMIN_LOGIN:
                userSession = userLoginFacade.loginByPhonePassword(loginRequest.getDeviceId(), loginRequest.getPhoneNumber(), loginRequest.getPassword(),AndriodLoginTypeEnum.getByCode(loginRequest.getLoginType()));
                break;
            case PHONE_LAST_FOUR:
                userSession = userLoginFacade.loginByPhoneLastFour(loginRequest.getDeviceId(), loginRequest.getPhoneNumber(),AndriodLoginTypeEnum.getByCode(loginRequest.getLoginType()));
                break;
            default:
                String errorMessage = String.format("不支持的用户登陆验证方式 deviceId=%s loginType=%s", loginRequest.getDeviceId(), loginRequest.getLoginType());
                log.error(errorMessage);
                throw new BizException(CommonError.ILLEGAL_PARAMETER, errorMessage);
        }

        DeviceLoginResponse response = new DeviceLoginResponse();
        response.setUserSession(userSession);

        return response;
    }

    /**
     * 登录成功回调
     */

    @PostMapping("/userLoginCallBack")
    public Result<Boolean> userLoginCallBack(@Validated @RequestBody CallbackDTO callbackDTO) {
        AndroidDeviceVO deviceByImei = deviceFacade.getDeviceByImei(callbackDTO.getImei());
         userLoginFacade.deviceLoginCallback(callbackDTO,deviceByImei);
        return Result.success(Boolean.TRUE);
    }


    @PostMapping("/list")
    public List<AndroidUserListResponse> listUser(@RequestBody AndroidUserListRequest request) {

        AndroidDeviceVO deviceDTO = deviceFacade.getDeviceByImei(request.getImei());
        List<UserQueryDTO> tmp = userQueryFacade.findByFirmId(deviceDTO.getFirmId());

        if (CollectionUtils.isEmpty(tmp)) {
            return Collections.emptyList();
        }

        return tmp.stream().map(userQueryDTOConverter::dto2Response).collect(Collectors.toList());
    }

    @Autowired
    private UserLoginFacade userLoginFacade;

    @Autowired
    private UserQueryFacade userQueryFacade;

    @Autowired
    private UserQueryDTOConverter userQueryDTOConverter;

    @Autowired
    private DeviceFacade deviceFacade;

}
